package com.um.pasantiaum.cliente;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.um.pasantiaum.empresa.productos.*;
import com.parse.ParseUser;
import com.um.pasantiaum.R;
import com.um.pasantiaum.objetos.Producto;
import com.um.pasantiaum.objetos.ProductoManager;
import com.um.pasantiaum.objetos.Usuario;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.Toast;

public class SelectProdFragment extends ListFragment {
	public final static String CLICKED_PRODUCT ="CLICKED_PRODUCT";
	public TextView descriptionView;
	public TextView nameView;
	public TextView priceView;
	ArrayList<Producto> mProductos = new ArrayList <Producto>();
	ParseUser temporal=null;
	ArrayList<Producto> compras = new ArrayList <Producto>();
	ArrayList<String> precios = new ArrayList<String>();
	String val1;
	Context a;
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
		setHasOptionsMenu(true);
		int max=0;
		
		Bundle bundle = this.getArguments();
		String UserId = bundle.getString(CLICKED_PRODUCT);
		
		
		
		ParseQuery<ParseUser> query = ParseUser.getQuery();
		ParseUser temp=null;
		try {
			temp = query.get(UserId);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//try {
			//List<ParseObject> objects = query.find();
			
			
				
			//temporal = objects.get(0).getParseUser("user");
			//}
			
		//	catch(ParseException e1) {
			//	Log.d("Productos", "Error: " + e1.getMessage());
				
		//	}
		ParseQuery<ParseObject> query2 = ParseQuery.getQuery("UserProductos");
		query2.whereEqualTo("usuario", temp);
		query2.whereEqualTo("isInStock", true);
		
		try {
			List<ParseObject> objects2 = query2.find();
			
			for (ParseObject po : objects2)
				mProductos.add(new Producto(po));
		} catch (ParseException e1) {
			Log.d("Productos", "Error: " + e1.getMessage());
		}
	
		
	
	       max= mProductos.size();
		AdminProductAdapter adapter = new AdminProductAdapter(mProductos);
		setListAdapter(adapter);


	}
	
	public class AdminProductAdapter extends ArrayAdapter<Producto> {
		int max=0;
		public AdminProductAdapter(ArrayList<Producto> prod) {
			super(getActivity(), 0, prod);
				max= prod.size();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = getActivity().getLayoutInflater().inflate(
						com.um.pasantiaum.R.layout.list_productos_empresa, null);
				convertView.requestFocus();
			}

			Producto p = getItem(position);
			final TextView nameView = (TextView) convertView
					.findViewById(com.um.pasantiaum.R.id.prod_nom);
			
			nameView.setText(p.getNombre());
			TextView descriptionView = (TextView) convertView
					.findViewById(com.um.pasantiaum.R.id.prod_des);
			descriptionView.setText(p.getDescripcion());
			TextView priceView = (TextView) convertView
					.findViewById(com.um.pasantiaum.R.id.prod_p);
			priceView.setText(p.getPrice().toString() + " $");
			Button b = (Button) convertView.findViewById(com.um.pasantiaum.R.id.ButtonViewCart);
			
			if(position!= max-1){
				b.setEnabled(false);	
				b.setVisibility(View.GONE);
			}
			b.setOnClickListener(new OnClickListener() {

				
				public void onClick(View view) {
					ArrayList <String> prods = new ArrayList<String>();
					for(int i =0;i< compras.size(); i++){
						prods.add(compras.get(i).getNombre());
					}
					Bundle bundle = new Bundle(0);
					bundle.putStringArrayList(CarritoFragment.CLICKED_PRODUCT, prods);
					bundle.putStringArrayList(CarritoFragment.CLICKED_PRODUCT1, precios);
					CarritoFragment newFragment = new CarritoFragment();
					newFragment.setArguments(bundle);
					// Replace whatever is in the fragment_container view with this fragment,
					// and add the transaction to the back stack
						FragmentTransaction transaction = getFragmentManager().beginTransaction();
						transaction.replace(R.id.container, newFragment);
						transaction.addToBackStack(null);

					// Commit the transaction
					transaction.commit();
			      }
			    	  
			          });
			// TextView email = (TextView) convertView.findViewById(com.um.pasantiaum.R.id.cantidad);
	    	 //email.setText("1 unidadd");
	    	
			ImageButton b2 = (ImageButton) convertView.findViewById(com.um.pasantiaum.R.id.agregar);
			b2.setTag(position);
			b2.setOnClickListener(new OnClickListener() {

				
				public void onClick(View view) {
					//View convertView = getActivity().getLayoutInflater().inflate(
						//	com.um.pasantiaum.R.layout.list_productos_empresa, null);
				//	convertView.requestFocus();
					//TextView email = (TextView) convertView.findViewById(com.um.pasantiaum.R.id.cantidad);
					Producto p = (Producto) ((AdminProductAdapter) getListAdapter())
							.getItem((Integer) view.getTag());
					Log.d("click","click1" + String.valueOf((Integer)view.getTag()));
			   	   //Your Toast with String val; 
			    	  
			    	   Toast toast = Toast.makeText( getContext(), "Se agregaron " 
			    	   + "1 lt" + "del producto " + p.getNombre(), Toast.LENGTH_LONG);

			    	   toast.show();
			    	   compras.add(p);
			    	   precios.add(String.valueOf(p.getPrice()));
			      }
			    	  
			          });
				
			
			
		
			
			
			return convertView;
		
	
		}}
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		Producto p = (Producto) ((AdminProductAdapter) getListAdapter())
				.getItem(position);
		/*Log.d("EmpresasFragment", p.getName());
		Intent i = new Intent(getActivity(), ProductosEmpresaActivity.class);
		//i.putExtra(ProductosEmpresaActivity.CLICKED_PRODUCT1, p.getUserId());
		Log.d("EmpresasFragment", "listo");
		startActivity(i);*/
		// Create new fragment and transaction
		
		
		
		
		
	

}
}
